package com.lovo.javafxComponent;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;

import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.skin.TableViewSkin;
import javafx.scene.layout.Pane;
import javafx.util.Callback;

/**
 * 表格组件
 * @author lenovo
 *
 */
public class LovoTable<T> extends TableView{
	/**
	 * 构造方法
	 * @param infoArray 信息字符串数组，每个元素格式为“姓名:Student.name”分别为列名，对应属性名
	 * @param x X坐标
	 * @param y Y坐标
	 * @param width 宽度
	 * @param height 高度
	 * @param container 加入容器
	 */
	public LovoTable(String[] infoArray,int x,int y ,
			int width,int height,Pane container) {

		for(int i=0;i<infoArray.length;i++) {
			String[] array = infoArray[i].split(":");
			
			TableColumn<T,String> c = new TableColumn<>(array[0]);
			c.setStyle("-fx-font-size: 16px");
			if(array.length == 2) {
				if(array[1].indexOf(".") == -1) {
					//绑定表头对应的属性
					c.setCellValueFactory(new PropertyValueFactory<T,String>(array[1]));
				}
				else {
					c.setCellValueFactory(getCallBackObj(array[1]));
				}
			}
			this.getColumns().add(c);
		}
		this.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

		this.setLayoutX(x);
		this.setLayoutY(y);
		this.setPrefWidth(width);
		this.setPrefHeight(height);
		container.getChildren().add(this);
	}
	
	private Callback getCallBackObj(String str) {
		// TODO Auto-generated method stub
		return new Callback<CellDataFeatures<T, String>, ObservableValue<String>>() {
		     public ObservableValue<String> call(CellDataFeatures<T, String> p) {
		         // p.getValue() returns the Person instance for a particular TableView row
		    	 ObservableValue<String> x = new ObservableValue<String>() {
 
					@Override 
					public void addListener(InvalidationListener listener) {}

					@Override
					public void removeListener(InvalidationListener listener) {}

					@Override
					public void addListener(ChangeListener<? super String> listener) {}

					@Override
					public void removeListener(ChangeListener<? super String> listener) {}

					@Override
					public String getValue() {
						String[] strArray = str.split("[.]");
						
						Object t = p.getValue();
						Class tclass = t.getClass();
						try {
							for(int i=0;i<strArray.length;i++) {
								Field f = tclass.getDeclaredField(strArray[i]);
								f.setAccessible(true);
								t = f.get(t);
								tclass = t.getClass();
							}
						}catch(Exception e) {
							e.printStackTrace();
						}
						
						return t.toString();
					}
		    		 
		    	 };
		    	 return x;
		     }
	};
	}

	/**
	 * 更新表格数据
	 * @param col 集合对象
	 */
	public void updateData(Collection<T> col) {
		if(col != null) {
			ObservableList<T> list = FXCollections.observableArrayList(col);
			this.setItems(list);
		}
	}
	
	/**
	 * 得到选中行对象
	 * @return 选中行对象
	 */
	public T getSelectItem() {
		Object t = this.getSelectionModel().getSelectedItem();
		return (T)t;
	}
}
